var customGrid = {
	init: function(options){
		options = options || {};
		options.url = options.url || 'getJson.json';
		options.grid = options.grid || '#grid-table';
		options.pager = options.pager || '#grid-pager';
		
		$(options.grid).jqGrid({
	        url: options.url,
	        mtype: "GET",
			styleUI : 'Bootstrap',
	        datatype: "json",
	        colModel: options.colModel,
			viewrecords: true,
	        height: 'auto',
	        gridview:true,
	        autowidth: true,
	        editurl: 'deleteJson.json',
	        rowNum: 15,
	        jsonReader:{
				root:"datarows",
				repeatitems:false
			},
	        pager: options.pager
	    });
	}, //init
	
	reload: function(options){
		options = options || {};
		options.grid = options.grid || '#grid-table';
		options.url = options.url || 'getJson.json';
		options.page = options.page || 1;
		var jqGrid = $(options.grid).jqGrid('setGridParam',{url:options.url,page:options.page});
		if(jqGrid){
			jqGrid.trigger("reloadGrid");
		}
	},
	
	reloadNoPage: function(options){
		options = options || {};
		options.grid = options.grid || '#grid-table';
		var jqGrid = $(options.grid).jqGrid('setGridParam',{url:options.url});
		if(jqGrid){
			jqGrid.trigger("reloadGrid");
		}
	},
	reloadSelf: function(options){
		options = options || {};
		options.grid = options.grid || '#grid-table';
		var $grid = $(options.grid);
	    url = $grid.jqGrid('getGridParam','url');
	    customGrid.reloadNoPage({url:url});
	}
	
};

function customActions(cellValue,options,rowObject){
	var id = options.rowId,
	    table = options.gid;
	    currentPage = $("#"+table).jqGrid("getGridParam","page");
	    editUrl = "preEditJson.json?id="+id;
	    delUrl = "deleteJson.json";
	    fullDelUrl = "delete.jspx";
	
	if(options.colModel.formatoptions && options.colModel.formatoptions.editUrl){
		editUrl = options.colModel.formatoptions.editUrl+id+"&returnPage="+currentPage;
	}
	
	if(options.colModel.formatoptions && options.colModel.formatoptions.delUrl){
		//delUrl = options.colModel.formatoptions.delUrl+id;
		delUrl = options.colModel.formatoptions.delUrl;
		fullDelUrl = delUrl;// + id;
	}
	
	var edit = '<div title="'+$.jgrid.regional['cn'].nav.edittitle+'" style="float: left; cursor: pointer; display: block;" class="ui-pg-div ui-inline-edit" data-url="'+editUrl+'" data-toggle="modal" data-target="#edit-modal" onmouseover="jQuery(this).addClass(\'ui-state-hover\');" onmouseout="jQuery(this).removeClass(\'ui-state-hover\')">'
	           +'<span class="fa fa-pencil"></span></div>';
	var del = '<div title="'+$.jgrid.regional['cn'].nav.edittitle+'" style="float: left; margin-left: 5px; display: block;" class="ui-pg-div ui-inline-del" data-grid="'+table+'" data-url="'+delUrl+'" data-id="'+id+'" onclick="deleteDialog(this)" onmouseover="jQuery(this).addClass(\'ui-state-hover\');" onmouseout="jQuery(this).removeClass(\'ui-state-hover\');">'
	           +'<span class="fa fa-trash"></span></div>';        
	/*
	var edit = "<div title=\""+$.jgrid.regional['cn'].nav.edittitle+"\" style=\"float: left; cursor: pointer; display: block;\" class=\"ui-pg-div ui-inline-edit\" onclick=\"redirect('"+editUrl+"','"+table+"',"+id+",false)\" onmouseover=\"jQuery(this).addClass('ui-state-hover');\" onmouseout=\"jQuery(this).removeClass('ui-state-hover')\">"
    +"<span class=\"ui-icon ui-icon-pencil\"></span></div>";
    var del = "<div title=\""+$.jgrid.regional['cn'].nav.edittitle+"\" style=\"float: left; margin-left: 5px; display: block;\" class=\"ui-pg-div ui-inline-del\" onclick=\"redirect('"+fullDelUrl+"','"+table+"',"+id+",true)\" onmouseover=\"jQuery(this).addClass('ui-state-hover');\" onmouseout=\"jQuery(this).removeClass('ui-state-hover');\">"
    +"<span class=\"ui-icon ui-icon-trash\"></span></div>";
    */
	if(options.colModel.formatoptions && options.colModel.formatoptions.canEdit!==undefined && options.colModel.formatoptions.canEdit===false){
		edit = "";
	}
	if(options.colModel.formatoptions && options.colModel.formatoptions.canDel!==undefined && options.colModel.formatoptions.canDel===false){
		del="";
	}
	return edit + del;
}

function deleteDialog(source){
	layer.confirm('确定要删除此记录吗？',{}, function(index){
		var $source = $(source),
		   url = $source.attr('data-url'),
		   grid = $source.attr('data-grid'),
		   id = $source.attr('data-id');
		if(url && id){
			$.post(url, {id:id}, function(result){
				if(result && result.status==1){
					var options = {
							grid: '#'+grid
					}
					customGrid.reloadSelf(options);
					layer.close(index);
				}else{
					layer.alert("删除记录出错");
					layer.close(index);
				}
			});
		}
	}, function(index){
		layer.close(index);
	});
}
